package com.example.mapboot.utils;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.alibaba.rocketmq.shade.io.netty.util.internal.StringUtil;
import com.example.mapboot.dto.CoordinateListDTO;
import com.example.mapboot.dto.Results;
import com.example.mapboot.dto.place.PlaceDTO;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

public class BaiduApiUtils {
    static String AK = "GAZ0zl8OetdriRpWStwoebZQP8mWnmpt"; // 百度地图密钥
    static String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiIzMmU2YmYwOTY5NWI4OGM4OTIzMWEwMGY1MzEyZDlkNCJ9.MLBpUJ4TxTh6oItNYhv4qGrlqgpO8qI87EHl0Pi-UlE";
    public static void main(String[] args) {

    }

    public static PlaceDTO getTag(String query,String tag,String region){
        String url = "https://api.map.baidu.com/place/v2/search?query="+query+"&tag="+tag+"&region="+region+"&output=json&ak="+AK;
        String s = RequestUtils.loadJSON(url);
        return JSON.parseObject(s,PlaceDTO.class);
    }

    public static String getWeather(String provice, String city, String area, String name){
        if(provice!=null && city!=null){
            String url = "https://www.douyacun.com/api/openapi/weather?province="+provice+"&city="+city+"&weather_type=forecast_hour&token="+token;
            return RequestUtils.loadJSON(url);
        }else{
            String url = "https://www.douyacun.com/api/openapi/weather?city="+name+"&weather_type=forecast_hour&token="+token;
            return RequestUtils.loadJSON(url);
        }
    }

    public static List<Results> getCoordinateList(String address){
        String url = "https://api.map.baidu.com/place/v2/search?query="+address+"&region=全国&output=json&ak="+AK;
        String json = RequestUtils.loadJSON(url);
        CoordinateListDTO coordinateListDTO = JSON.parseObject(json, CoordinateListDTO.class);
        if(coordinateListDTO.getStatus()==0){
            return coordinateListDTO.getResults();
        }else{
            return Arrays.asList();
        }
    }

    // 调用百度地图API根据地址，获取坐标
    public static String getCoordinate(String address) {
        if (address != null && !"".equals(address)) {
            address = address.replaceAll("\\s*", "").replace("#", "栋");
            String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + AK;
            String json = RequestUtils.loadJSON(url);
            if (json != null && !"".equals(json)) {
                // JSONObject obj = JSONObject.fromObject(json);
                JSONObject obj = JSON.parseObject(json);
                if ("0".equals(obj.getString("status"))) {
                    double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
                    double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
                    DecimalFormat df = new DecimalFormat("#.######");
                    return df.format(lng) + "," + df.format(lat);
                }
            }
        }
        return null;
    }
}
